'use strict';

const Controller = require('egg').Controller;
/**
 * @Controller 用户管理模块
 */
class UserController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }

  /**
   * @Summary 获取用户列表
   * @Description 获取用户列表
   * @Router get /user/list
   * @Request query string name 名称
   * @Request query number page 页码
   * @Request query number limit 每页条数
   * @Response 200 userListRes
   * */
  async getList() {
    const { name, page, limit } = this.ctx.request.query;
    const { rows: list, count } = await this.service.user.getList({
      name,
      page,
      limit,
    });
    this.ctx.body = {
      code: 20000,
      count,
      list,
    };
  }

  /**
   * @Summary 创建用户
   * @Description 创建用户
   * @Router post /user/create
   * @Request body createUserParams
   * */
  async create() {
    const { name, age, sex, birth, addr } = this.ctx.request.body;
    const user = await this.service.user.create({
      name,
      age,
      sex,
      birth,
      addr,
    });
    this.ctx.body = {
      code: 20000,
      data: user,
    };
  }

  /**
   * @Summary 更新用户
   * @Description 更新用户
   * @Router post /user/update
   * @Request body updateUserParams
   * */
  async update() {
    const { id, name, addr, age, birth, sex } = this.ctx.request.body;
    const result = await this.service.user.update({
      id,
      name,
      addr,
      age,
      birth,
      sex,
    });

    this.ctx.body = {
      code: result ? 20000 : -999,
      message: result ? '更新成功' : '参数不正确',
    };
  }

  /**
   * @Summary 删除用户
   * @Description 删除用户
   * @Router post /user/delete
   * @Request body deleteUserParams
   * */
  async delete() {
    const { id } = this.ctx.request.body;
    const result = await this.service.user.delete(id);
    this.ctx.body = {
      code: result ? 20000 : -999,
      message: result ? '删除成功' : '参数不正确',
    };
  }
}

module.exports = UserController;
